package com.lyf.task.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lyf.task.domain.User;
import com.lyf.task.service.RFIDUtilService;
import com.lyf.task.service.UserService;

@Controller
public class UserController {

	@Autowired
	private UserService userServiceImpl;

	@Autowired
	private RFIDUtilService rfidUtilServiceImpl;

	@RequestMapping(value = "/userall.html")
	public String getAllUser(Model model) {

		// rfidUtilServiceImpl.write(data);

		List<User> list = userServiceImpl.getAllUser();
		model.addAttribute("userList", list);
		return "user";
	}

	@RequestMapping(value = "index.html")
	public String index(String pid) {
		return "redirect:/productList.html";
	}

	@RequestMapping(value = "quit.html")
	public String quit(HttpServletRequest request) {
		request.getSession().invalidate();
		return "redirect:/login.html";
	}

	@RequestMapping(value = "login.html")
	public String login() {
		return "login";
	}

	@RequestMapping(value = "regist.html")
	public String register() {
		return "regist";
	}

	@RequestMapping(value = "registed.html", method = RequestMethod.POST)
	public String registered(User user) {
		System.out.println(user.getRealName());
		userServiceImpl.insertUser(user);
		return "login";
	}

	@RequestMapping(value = "logined.html", method = RequestMethod.POST)
	public String logined(Model model, HttpSession httpSession, User user) {

		int staut = userServiceImpl.loginUser(user);

		if (staut == 200) {
			httpSession.setAttribute("user", user);
			return "redirect:/productList.html";
		} else {
			return "relogin";
		}

	}
}
